// pages/mima/mima.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    button: 'true',
    is_zhuanfa:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options,'分享参数')
    if (options.code){
      this.setData({
        is_zhuanfa:true,
        RefereeCode: options.code,
        NickName: options.NickName
      })
    }
    this.setData({
      userId: wx.getStorageSync('userId'),
    })
    this.zhuce_xieyi();

  },
  // 复选框
  checkboxChange: function (e) {
    console.log(e.detail.value)
  },
  //注册协议
  zhuce_xieyi:function(){
    var that=this;
    wx.request({
      url: app.globalData.apiUrl + 'user/zhuce_xieyi',
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        var article = that.delHtmlTag(res.data.article['content'])
        var article2 = that.delHtmlTag(res.data.article2['content'])

        res.data.article['content'] = article; 
        res.data.article2['content'] = article2; 

        that.setData({
          info: res.data
        })
      }
    })
  },
  delHtmlTag: function (msg) {
    var msg = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
    // msg = msg.replace(/[|]*\n/, '') //去除行尾空格
    msg = msg.replace(/&nbsp;/ig, ''); //去掉npsp
    return msg;
  },
  blurPhone: function (e) {
    console.log(e)
    var phone = e.detail.value;
    let that = this
    if (!(/^1[34578]\d{9}$/.test(phone)) || phone.length > 11) {
        // wx.showToast({
        //   title: '手机号有误',
        //   icon: 'success',
        //   duration: 2000
        // })
      this.setData({
        ajxtrue: false,
      })
    } else {
      this.setData({
        ajxtrue: true,
        phone: phone,
      })

    }
  },
  get_yanzheng:function(){
    var that=this;
    if (that.data.phone == "" || that.data.ajxtrue !== true) {
      wx.showToast({
        title: '手机号有误',
        icon: 'success',
        duration: 2000
      })
      return;
    }else{
      app.get_yanzheng(that.data.phone,function (data) {
        // var that=this;
        that.setData({
          is_send: 1
        })
        var flag = true;
        var n = 60;
        var current = 60;
        that.setData({
          wait_time: n,
          // time_out: 'block',
        })
        var timer = null;
        timer = setInterval(function () {
          n--;
          that.setData({
            wait_time: n,
          })
          if (n <= 0) {
            clearInterval(timer);
            n = current;
            var nowflag = true;
            var flag = false;
            that.setData({
              is_send: 0,
              // gety_but: false
            })
          }
        }, 1000);
        if (data !== "") {
          that.setData({
            yanzhengma: data,
          });
        }
      })
    }
    
  },
  
  formSubmit: function(e) {
    console.log(e)

    var that = this;
    var formData = e.detail.value;
    if (formData['Code'] == "" || formData['PassWord_2'] == "" || formData['yanzhengma'] == "" || formData['RefereeCode'] == "") {
      wx.showToast({
        title: '必填',
        icon: 'none'
      })
      return;
    }
    if (formData['LoginPassword'] !== formData['repassword']) {
      wx.showToast({
        title: '密码不一致',
        icon: 'none'
      })
      return;
    }
    console.log(that.data.yanzhengma)
    if (Number(formData['yanzhengma']) !== that.data.yanzhengma) {
      wx.showToast({
        title: '验证码不正确',
        icon: 'none'
      })
      return;
    }
    that.setData({
      button: 'false'
    })
    // console.log(that.data.button)
    // return ;
    wx.request({
      url: app.globalData.apiUrl + 'user/register_do',
      data: formData,
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',

            success: function() {
              setTimeout(function() {
                wx.navigateTo({
                  url: '/pages/login/login',
                })
              }, 2000);
            }
          })
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
        that.setData({
          button: 'true'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.alertview = this.selectComponent('#alertview')
  },
  // 按钮点击事件
  clickMe: function() {
  
    this.alertview.showAlertView();
    this.setData({
      xieyi: this.data.info.article['content']
    })
  }, 
   clicksi_Me: function() {

    this.alertview.showAlertView();
     this.setData({
       xieyi: this.data.info.article2['content']
     })
  }, 
  // 确定事件
  confirmEvent: function() {
    this.alertview.hideAlertView();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  bandLogin:function(){
    wx.navigateTo({
      url:'/pages/login/login'
    })
  }
  
})